/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.alipay.demo.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.*;
import com.alipay.api.internal.util.StringUtils;
import com.alipay.api.request.AlipayOpenAppDeliveryInfoQueryRequest;
import com.alipay.api.request.AlipayOpenAppDeliveryInfoSyncRequest;
import com.alipay.api.response.AlipayOpenAppDeliveryInfoQueryResponse;
import com.alipay.api.response.AlipayOpenAppDeliveryInfoSyncResponse;
import com.alipay.demo.vo.DeliveryTemplateQueryRequest;
import com.alipay.demo.vo.DeliveryTemplateQueryResponse;
import com.alipay.demo.service.ItemDeliveryService;
import com.alipay.demo.util.AlipayApiCallback;
import com.alipay.demo.util.AlipayApiTemplate;
import com.alipay.demo.util.AlipaySdkUtil;
import com.alipay.demo.vo.ItemDeliverySyncRequest;
import com.alipay.demo.vo.OpenResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jishupei.jsp
 * @version : ItemDeliveryServiceImpl, v0.1 2024年05月17日 3:16 下午 jishupei.jsp Exp $
 */
@Service
public class ItemDeliveryServiceImpl implements ItemDeliveryService {

    @Autowired
    private AlipaySdkUtil alipaySdkUtil;

    @Override
    public OpenResponse<DeliveryTemplateQueryResponse> queryTemplate(String deliveryType) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<DeliveryTemplateQueryResponse, DeliveryTemplateQueryResponse>() {
            @Override
            public DeliveryTemplateQueryResponse process() throws AlipayApiException {
                // 构造请求参数以调用接口
                DeliveryTemplateQueryRequest request = new DeliveryTemplateQueryRequest();
                AlipayOpenAppDeliveryTemplateQueryModel model = new AlipayOpenAppDeliveryTemplateQueryModel();

                // 设置履约类型
                model.setDeliveryType(deliveryType);

                request.setBizModel(model);
                return alipaySdkUtil.execute(request);
            }

            @Override
            public DeliveryTemplateQueryResponse getData(DeliveryTemplateQueryResponse response) {
                return response;
            }

            @Override
            public String getApiName() {
                return "alipay.open.app.delivery.template.query";
            }
        });
    }

    @Override
    public OpenResponse<String> sync(ItemDeliverySyncRequest syncRequest) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<String, AlipayOpenAppDeliveryInfoSyncResponse>() {
            @Override
            public AlipayOpenAppDeliveryInfoSyncResponse process() throws AlipayApiException {
                // 构造请求参数以调用接口
                AlipayOpenAppDeliveryInfoSyncRequest request = new AlipayOpenAppDeliveryInfoSyncRequest();
                AlipayOpenAppDeliveryInfoSyncModel model = new AlipayOpenAppDeliveryInfoSyncModel();

                // 设置履约类型
                model.setDeliveryType(syncRequest.getDeliveryType());

                // 设置履约信息名称
                model.setDeliveryName(syncRequest.getDeliveryName());

                if (!StringUtils.isEmpty(syncRequest.getDeliveryId())) {
                    // 设置履约信息ID
                    model.setDeliveryId(syncRequest.getDeliveryId());
                }

                OpenResponse<DeliveryTemplateQueryResponse> templateResponse = queryTemplate(syncRequest.getDeliveryType());
                if (!"10000".equals(templateResponse.getCode())) {
                    throw new AlipayApiException("查询普通商品履约模板失败");
                }
                List<AttributeVO> attributes = templateResponse.getData().getAttrs();
                if (!CollectionUtils.isEmpty(attributes)) {
                    // 设置履约属性
                    List<AppItemAttrVO> attrs = new ArrayList<>();
                    for (AttributeVO attr : attributes) {
                        AppItemAttrVO appItemAttrVO = new AppItemAttrVO();
                        appItemAttrVO.setAttrKey(attr.getKey());
                        switch (attr.getKey()) {
                            case "delivery_name":
                                // 履约模板名称
                                appItemAttrVO.setAttrValue(syncRequest.getDeliveryName());
                                break;
                            // -----BEGIN 电商卖货 -----
                            case "delivery_free_ship":
                                // 是否包邮
                                appItemAttrVO.setAttrValue("1");
                                break;
                            // -----END 电商卖货 -----

                            // -----BEGIN 外卖到家 -----
                            case "delivery_price_rule":
                                // 配送门槛
                                appItemAttrVO.setAttrValue("{\"limit\":\"1\",\"price\":\"2000\"}");
                                break;
                            case "delivery_timeliness_rule":
                                // 配送时效，5公里30分钟
                                appItemAttrVO.setAttrValue("[{\"delivery_rules\":[{\"distance\":\"5\",\"delivery_time\":\"30\"},{\"distance\":\"10\",\"delivery_time\":\"60\"}],\"type\":\"MINUTE\"}]");
                                break;
                            case "delivery_radius":
                                // 配送半径
                                appItemAttrVO.setAttrValue("10");
                                break;
                            case "delivery_fee":
                                // 配送费，2：无配送费
                                appItemAttrVO.setAttrValue("2");
                                break;
                            case "delivery_time":
                                // 配送时间
                                appItemAttrVO.setAttrValue("{\"days_of_week\":[\"6\",\"7\"],\"start_time\":\"08:00:00\",\"end_time_type\":\"SAME_DAY\",\"end_time\":\"20:00:00\"}");
                                break;
                            // -----END 外卖到家 -----
                        }
                        attrs.add(appItemAttrVO);
                    }
                    model.setAttrs(attrs);
                }

                request.setBizModel(model);
                return alipaySdkUtil.execute(request);
            }

            @Override
            public String getData(AlipayOpenAppDeliveryInfoSyncResponse response) {
                return response.getDeliveryId();
            }

            @Override
            public String getApiName() {
                return "alipay.open.app.delivery.info.sync";
            }
        });
    }

    @Override
    public OpenResponse<AlipayOpenAppDeliveryInfoQueryResponse> queryInfo(String deliveryType, List<String> deliveryIds) {
        return AlipayApiTemplate.execute(new AlipayApiCallback<AlipayOpenAppDeliveryInfoQueryResponse, AlipayOpenAppDeliveryInfoQueryResponse>(){
            @Override
            public AlipayOpenAppDeliveryInfoQueryResponse process() throws AlipayApiException {
                // 构造请求参数以调用接口
                AlipayOpenAppDeliveryInfoQueryRequest request = new AlipayOpenAppDeliveryInfoQueryRequest();
                AlipayOpenAppDeliveryInfoQueryModel model = new AlipayOpenAppDeliveryInfoQueryModel();

                if (!StringUtils.isEmpty(deliveryType)) {
                    // 设置履约类型
                    model.setDeliveryType(deliveryType);
                }

                if(!CollectionUtils.isEmpty(deliveryIds)){
                    // 设置履约信息ID列表
                    model.setDeliveryIds(deliveryIds);
                }

                request.setBizModel(model);
                return alipaySdkUtil.execute(request);
            }

            @Override
            public AlipayOpenAppDeliveryInfoQueryResponse getData(AlipayOpenAppDeliveryInfoQueryResponse response) {
                return response;
            }

            @Override
            public String getApiName() {
                return "alipay.open.app.delivery.info.query";
            }
        });
    }
}
